// 获取当前时间
exports.getLocalTimeString = function () {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, "0");
  const day = String(now.getDate()).padStart(2, "0");
  const hour = String(now.getHours()).padStart(2, "0");
  const minute = String(now.getMinutes()).padStart(2, "0");
  const second = String(now.getSeconds()).padStart(2, "0");
  const localTimeString = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
  return localTimeString;
};
// 将城市数据转为树状结构
exports.generateTree2 = (
  list,
  rootId = "000000",
  { idName = "id", parentIdName = "parentid", childName = "childern" } = {}
) => {
  if (!Array.isArray(list)) {
    new Error("type only Array");
    return list;
  }
  const objMap = {}; //暂存数组以 id 为 key的映射关系
  const result = []; // 结果

  for (const item of list) {
    const id = item[idName];
    const parentid = item[parentIdName];

    // 该元素有可能已经放入map中，（找不到该项的parentId时 会先放入map
    objMap[id] = !objMap[id] ? item : { ...item, ...objMap[id] };

    const treeItem = objMap[id]; // 找到映射关系那一项（注意这里是引用）

    if (parentid === rootId) {
      // 已经到根元素则将映射结果放进结果集
      result.push(treeItem);
    } else {
      // 若父元素不存在，初始化父元素
      if (!objMap[parentid]) {
        objMap[parentid] = [];
      }

      // 若无该根元素则放入map中
      if (!objMap[parentid][childName]) {
        objMap[parentid][childName] = [];
      }
      objMap[parentid][childName].push(treeItem);
    }
  }
  return result;
};
